#include "SoundManager.h"

#include <assert.h>
#include "Audio.h"

Audio* s_audioController = NULL;
static Audio* sharedAudioController()
{
	if (! s_audioController)
	{
		s_audioController = new Audio;
		s_audioController->Initialize();
		s_audioController->CreateResources();
	}

	return s_audioController;
}

SoundManager::SoundManager()
{
}

SoundManager::~SoundManager()
{
}

void SoundManager::Init()
{
	if(instance == NULL)
	{
		instance = new SoundManager();
	}
}

void SoundManager::end()
{
	sharedAudioController()->StopBackgroundMusic(true);
	sharedAudioController()->StopAllSoundEffects();
	sharedAudioController()->ReleaseResources();
}

void SoundManager::render()
{
	sharedAudioController()->Render();
}

//////////////////////////////////////////////////////////////////////////
// BackgroundMusic
//////////////////////////////////////////////////////////////////////////

void SoundManager::playBackgroundMusic(const char* pszFilePath, bool bLoop)
{
	if (! pszFilePath)
	{
		return;
	}

	sharedAudioController()->PlayBackgroundMusic(pszFilePath, bLoop);
}

void SoundManager::stopBackgroundMusic(bool bReleaseData)
{
	sharedAudioController()->StopBackgroundMusic(bReleaseData);
}

void SoundManager::pauseBackgroundMusic()
{
	sharedAudioController()->PauseBackgroundMusic();
}

void SoundManager::resumeBackgroundMusic()
{
	sharedAudioController()->ResumeBackgroundMusic();
}

void SoundManager::rewindBackgroundMusic()
{
	sharedAudioController()->RewindBackgroundMusic();
}

bool SoundManager::willPlayBackgroundMusic()
{
	return false;
}

bool SoundManager::isBackgroundMusicPlaying()
{
	return sharedAudioController()->IsBackgroundMusicPlaying();
}

//////////////////////////////////////////////////////////////////////////
// effect function
//////////////////////////////////////////////////////////////////////////

unsigned int SoundManager::playEffect(const char* pszFilePath, bool bLoop)
{
	unsigned int sound;
	sharedAudioController()->PlaySoundEffect(pszFilePath, bLoop, sound);

	return sound;
}

void SoundManager::stopEffect(unsigned int nSoundId)
{
	sharedAudioController()->StopSoundEffect(nSoundId);
}

void SoundManager::preloadEffect(const char* pszFilePath)
{
	sharedAudioController()->PreloadSoundEffect(pszFilePath);
}

void SoundManager::pauseEffect(unsigned int nSoundId)
{
	sharedAudioController()->PauseSoundEffect(nSoundId);
}

void SoundManager::resumeEffect(unsigned int nSoundId)
{
	sharedAudioController()->ResumeSoundEffect(nSoundId);
}

void SoundManager::pauseAllEffects()
{
	sharedAudioController()->PauseAllSoundEffects();
}

void SoundManager::resumeAllEffects()
{
	sharedAudioController()->ResumeAllSoundEffects();
}

void SoundManager::stopAllEffects()
{
	sharedAudioController()->StopAllSoundEffects();
}

void SoundManager::preloadBackgroundMusic(const char* pszFilePath)
{

}

void SoundManager::unloadEffect(const char* pszFilePath)
{
	sharedAudioController()->UnloadSoundEffect(pszFilePath);
}

//////////////////////////////////////////////////////////////////////////
// volume interface
//////////////////////////////////////////////////////////////////////////

float SoundManager::getBackgroundMusicVolume()
{
	return sharedAudioController()->GetBackgroundVolume();
}

void SoundManager::setBackgroundMusicVolume(float volume)
{
	sharedAudioController()->SetBackgroundVolume((volume<=0.0f)? 0.0f : volume);
}

float SoundManager::getEffectsVolume()
{
	return sharedAudioController()->GetSoundEffectVolume();
}

void SoundManager::setEffectsVolume(float volume)
{
	sharedAudioController()->SetSoundEffectVolume((volume<=0.0f)? 0.0f : volume);
}